package com.SmartCare.controller.client;

import com.SmartCare.common.Result;
import com.SmartCare.common.dto.homeInfo.SuggestionDTO;
import com.SmartCare.service.ImageService;
import com.SmartCare.service.homeInfo.SuggestionService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/suggestions")
@Api(tags = "C端建议箱接口")
public class SuggestController {
    @Resource
    private SuggestionService suggestionService;
    @Resource
    ImageService imageService;

    @Value("${files.upload.path}/suggestImg/")
    private String fileUploadPath;

    /**
     * 意见箱添加意见
     */
    @PostMapping("/new")
    public Result addNewSuggestion(@RequestBody SuggestionDTO suggestionDto){
        return suggestionService.addNewSuggestion(suggestionDto);
    }
    //添加图片
    @PostMapping("/upload")
    @Operation(summary = "上传图片文件", description = "将返回该图片的下载地址")
    public Result addImage(@RequestParam List<MultipartFile> file) {
        return imageService.upload(file, fileUploadPath, "suggest");
    }

    //下载图片
    @GetMapping("/{fileUuid}")
    @Operation(summary = "上传图片文件", description = "直接访问该路径可下载图片")
    public void download(@PathVariable String fileUuid, HttpServletResponse response) throws IOException {
        imageService.download(fileUuid, fileUploadPath, response);
    }
}
